import request from '../utils/request'
import qs from 'qs';
import {Toast} from 'antd-mobile';
import {routerRedux} from 'dva/router'
import {getRedirectPath} from '../utils/path'
let index = {
    namespace: 'user',
    state: {
        username:'',
        //password:'',
        type:'',
        redirectPath:'', 
        avatar:'',
        desc:'',
        company:'',
        money:'',
        title:'',
        user_id:'',
        otherAvatar:'',
        userList:[],
    },
    effects:{
        *register({payload},{call,put}){
            //console.log(payload);
            let res = yield call(request('/register',{method:'post',data:qs.stringify(payload)}));
            //console.log(res);
            if(res.data.code===0){
                Toast.info('注册成功',2);
                yield put(routerRedux.push('/login'));
            }else{
                Toast.info('网络繁忙，请稍后再试',2);
            }
        },
        *login({payload},{call,put}){
            //console.log(payload);
            let res = yield call(request('/login',{method:'post',data:qs.stringify(payload)}));
            console.log(res);
            //判断登录是否成功
            if(res.data.code === 2){
                Toast.info('账号未注册',2);
            }else if(res.data.code === 1){
                Toast.info('网络繁忙，请稍后再试',2);
            }else if(res.data.code ===0){
                Toast.info('登录成功',2);
                //保存数据
                yield put({type:'saveInfo',payload:res.data.data});
                //跳转页面
                //这边不需要跳转，需要根据参数来跳转
                //yield put(routerRedux.push('/'));
            }
        },
        *skip2Login({payload},{call,put}){
            yield put(routerRedux.push('/register'));
        },
        *getInfo({payload},{call,put}){
            let res = yield call(request('/info'));
            if (res.status == 200) {
                if (res.data.code == 0) {
                // 有登录信息de
                //this.props.loadData(res.data.data);
                //console.log('123');
                //console.log(res);
                
                yield put({type:'loadData',payload:res});
               }
            }
            else {
                yield put(routerRedux.push('/login'));
                }
        },
        
        //更新数据
        *updataBossInfo({payload},{call,put}){
            //console.log(payload);
            //console.log('123')
            let res = yield call(request('/updateBossInfo',{method:'post',data:qs.stringify(payload)}));
            //console.log(res);
            if(res.status ==200){
                if(res.data.code ==0){
                    yield put({type:'loadData',payload:res});
                }else if(res.data.code == 1){
                    Toast.info('您还未登录',2);
                    yield put(routerRedux.push('/login'));
                }
            }else{
                Toast.info('网络繁忙，请稍后再试');
            }
            
        },
        *updataGeniusInfo({payload},{call,put}){
            let res = yield call(request('/updateGeniusInfo',{method:'post',data:qs.stringify(payload)}));
            //console.log(res);
            if(res.status ==200){
                if(res.data.code ==0){
                    yield put({type:'loadData',payload:res});
                }else if(res.data.code == 1){
                    Toast.info('您还未登录',2);
                    yield put(routerRedux.push('/login'));
                }
            }else{
                Toast.info('网络繁忙，请稍后再试');
            }
        },
        *getAvatar({payload},{call,put}){
            //console.log(payload);
            let res = yield call(request('/getAvatar',{method:'post',data:qs.stringify({_id:payload})}));
            //console.log(res);
            yield put({type:'otherAvatar',payload:res.data.data.avatar})
        },
        *getAllUser({payload},{call,put}){
            let res = yield call(request('/getAllUser'));
            //console.log(res);
            yield put({type:'initUserList',payload:res.data.data});
        },
        *getInfos({payload},{call,put}){
            console.log(payload);
            let res = yield call(request('/infos',{method:'post',data:qs.stringify({_id:payload})}));
            console.log(res);
            yield put({type:'loadData2',payload:{data:res.data.data,otherAvatar:res.data.otherAvatar}});
        }

    },
    reducers:{
        saveInfo(state,{payload}){
            //console.log(payload)
            return{
                username:payload.user,
                //password:payload.password,
                type:payload.type,
                redirectPath:getRedirectPath(payload),
            }
        },
        loadData(state,{payload}){
            //console.log(payload.data);
            return{
                username:payload.data.data.user,
                type:payload.data.data.type,
                redirectPath:getRedirectPath(payload.data.data),
                avatar:payload.data.data.avatar,
                desc:payload.data.data.desc,
                company:payload.data.data.company,
                title:payload.data.data.title,
                money:payload.data.data.money,
                user_id:payload.data.data._id,
                
            }
        },
        logout(state,{payload}){
            return{
                username:'',
                type:'',
                redirectPath:'/login',
                avatar:'',
                desc:'',
                company:'',
                title:'',
                money:'',
            }
        },
        loadData2(state,{payload}){
            return{
                otherAvatar:payload.otherAvatar,
                username:payload.data.user,
                type:payload.data.type,
                redirectPath:getRedirectPath(payload), 
                avatar:payload.data.avatar,
                desc:payload.data.desc,
                company:payload.data.company,
                money:payload.data.money,
                title:payload.data.title,
                user_id:payload.data._id,
                userList:state.userList,
            }
        },
        otherAvatar(state,{payload}){
            return{
                otherAvatar:payload,
                username:state.username,
                type:state.type,
                redirectPath:state.redirectPath, 
                avatar:state.avatar,
                desc:state.desc,
                company:state.company,
                money:state.money,
                title:state.title,
                user_id:state.user_id,
            }
        },
        initUserList(state,{payload}){
            return{
                otherAvatar:state.otherAvatar,
                username:state.username,
                type:state.type,
                redirectPath:state.redirectPath, 
                avatar:state.avatar,
                desc:state.desc,
                company:state.company,
                money:state.money,
                title:state.title,
                user_id:state.user_id,
                userList:payload
            }
        }
    }
}
export default index;